<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/14
 * Time: 14:11
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class TechnicalCompanyModel
{
    const TABLE = 'technical_company';

    /**
     * @param array $data
     * @return mixed
     */
    public function lists(array $data)
    {
        $limit = config('yucheng.limit');
        $start = 0;
        if (!empty($data['length'])) {
            $limit = $data['length'];
        }
        if (!empty($data['page'])) {
            $start = ($data['page'] - 1) * $limit;
        }

        $result = DB::table(self::TABLE)
            ->where(function ($query) use ($data) {
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(function ($query1) use ($data) {
                        $query1->where(self::TABLE . '.chargePerson', 'like', '%' . $data['search'] . '%')->orWhere('name', 'like', '%' . $data['search'] . '%');
                    });
                }
            })
            ->orderBy('order', 'desc')->offset($start)->limit($limit)
            ->select(self::TABLE . '.*')
            ->get()->toArray();
        return $result;
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function countLists(array $data)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($data) {
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(function ($query1) use ($data) {
                        $query1->where(self::TABLE . '.chargePerson', 'like', '%' . $data['search'] . '%')->orWhere('name', 'like', '%' . $data['search'] . '%');
                    });
                }
            })
            ->count();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        return DB::table(self::TABLE)->insertGetId($data);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }


}